<template>
	<view>
		<view class="uni-header">
			<view class="uni-group">
				<view class="uni-title"></view>
				<view class="uni-sub-title"></view>
			</view>
			<view class="uni-group">
				<input class="uni-search" type="text" v-model="query" placeholder="请输入搜索内容" />
				<button class="uni-button" type="default" size="mini" @click="search">搜索</button>
			</view>
		</view>
		<view class="uni-container">
			<!-- :collection="collectionList" -->
			<unicloud-db ref="udb" collection="userinfo" :options="options" :where="where" field="_id,inviteCode,nickname,avatar,phone,coin,idcard,realname,fen,createtime,accountstatus,zhifubao" page-data="replace"
			 :orderby="orderby" :getcount="true" :page-size="options.pageSize" :page-current="options.pageCurrent"
			 v-slot:default="{data,pagination,loading,error}">
				<uni-table :loading="loading" :emptyText="error.message || '没有更多数据'" border stripe>
					<uni-tr>
						<uni-th align="center">ID号</uni-th>
						<uni-th align="center">邀请码</uni-th>
						<uni-th align="center">昵称</uni-th>
						<uni-th align="center">头像</uni-th>
						<uni-th align="center">手机号</uni-th>
						<uni-th align="center">账号状态</uni-th>
						<uni-th align="center">金币</uni-th>
						<uni-th align="center">身份证号</uni-th>
						<uni-th align="center">真实姓名</uni-th>
            <uni-th align="center">支付宝账号</uni-th>
						<uni-th align="center">可提现金额</uni-th>
						<uni-th width="170" align="center">注册时间</uni-th>
						<uni-th width="204" align="center">操作</uni-th>
					</uni-tr>
					<uni-tr v-for="(item,index) in data" :key="index">
						<uni-td align="center">{{item._id}}</uni-td>
						<uni-td align="center">{{item.inviteCode}}</uni-td>
						<uni-td align="center">{{item.nickname}}</uni-td>
						<uni-td align="center">
							<image :src="item.avatar" mode="" style="width: 80rpx;height: 80rpx;"></image>
						</uni-td>
						<uni-td align="center">{{item.phone}}</uni-td>
						<!-- <uni-td align="center" v-if="item._id[0]&&item._id[0].status">
							{{ item._id[0].status == 4 ? '账号已注销' : '正常' }}</uni-td>
						<uni-td align="center" v-else>{{ item.accountstatus == 1 ? '账号已封禁' : '正常' }}</uni-td> -->
						<uni-td align="center">{{ item.accountstatus == 1 ? '账号已封禁' : '正常' }}</uni-td>
						<uni-td align="center">{{item.coin?(parseInt(item.coin)/1000):0}}</uni-td>
						<uni-td align="center">{{item.idcard}}</uni-td>
						<uni-td align="center">{{item.realname}}</uni-td>
            <uni-td align="center">{{item.zhifubao}}</uni-td>
						
						<uni-td align="center">{{item.fen?(parseInt(item.fen)/1000):0}}</uni-td>
						<uni-td align="center">
							<uni-dateformat :date="item.createtime * 1000" :threshold="[0, 0]" />
						</uni-td>
						<uni-td align="center">
							<view class="uni-group">
								<button @click="item.accountstatus==0?confirmDelete(item):confirmOpenUser(item)" class="uni-button" size="mini"
									:type="item.accountstatus==0?'primary':'warn'">{{item.accountstatus==0?'封号':'解封'}}</button>
								<!-- accountstatus 0未封号, 1封号   status  0 正常 1 禁用
								<button  @click="confirmOpenUser(item)" v-if="item._id[0]&&item._id[0].status" class="uni-button" size="mini"
									type="primary">{{item._id[0].status === 4?'恢复已注销账号':'账号正常'}}</button>
								<button v-else @click="confirmDelete(item)" class="uni-button" size="mini"
									type="warn">{{item.accountstatus==0?'封号':'解封'}}</button> -->
							</view>
						</uni-td>
					</uni-tr>
				</uni-table>
				<view class="uni-pagination-box">
					<uni-pagination show-icon :page-size="pagination.size" v-model="pagination.current" :total="pagination.count"
					 @change="onPageChanged" />
				</view>
			</unicloud-db>
		</view>
	</view>
</template>

<script>
	const db = uniCloud.database()
	// 表查询配置
	const dbCollectionName = 'userinfo,uni-id-users'
	const dbOrderBy = 'createtime desc' // 排序字段
	const dbSearchFields = ['_id', "nickname", "phone"] // 支持模糊搜索的字段列表
	const dbField = '_id{_id,status},inviteCode,nickname,avatar,phone,coin,idcard,realname,fen,createtime,accountstatus'
	// 分页配置
	const pageSize = 15
	const pageCurrent = 1

	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				collectionList: [
					db.collection('userinfo').getTemp(),
					db.collection('uni-id-users').getTemp()
				],
				query: '',
				where: '',
				field:dbField,
				orderby: dbOrderBy,
				collectionName: dbCollectionName,
				options: {
					pageSize,
					pageCurrent
				}
			}
		},
		computed: {
			...mapState('user', ['userInfo']),
		},
		methods: {
			getWhere() {
				const query = this.query.trim()
				if (!query) {
					return ''
				}
				const queryRe = new RegExp(query, 'i')
				return dbSearchFields.map(name => queryRe + '.test(' + name + ')').join(' || ')
			},
			search() {
				const newWhere = this.getWhere()
				const isSameWhere = newWhere === this.where
				this.where = newWhere
				if (isSameWhere) { // 相同条件时，手动强制刷新
					this.loadData()
				}
			},
			loadData(clear = true) {
				this.$refs.udb.loadData({
					clear
				})
			},
			onPageChanged(e) {
				this.$refs.udb.loadData({
					current: e.current
				})
			},
			confirmDelete(item) {
				uni.showModal({
					title: '提示',
					content: '确定' + (item.accountstatus?'解封':'封禁') + '此用户吗？',
					showCancel: true,
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						console.log("res: ", res);
						if (res.confirm) {
							uni.showLoading({
								title: '处理中...',
								mask: false
							});
							
							uniCloud.callFunction({
								name: 'uni-admin',
								data: {
									action: "admin/fenghao",
									data: {
										fenghaoid: item._id
									}
								}
							}).then((res) => {
								console.log(res, "update--------------")
								this.loadData()
								uni.hideLoading();
								uni.showToast({
									title: "操作成功",
									icon: 'none',
									duration: 2000
								})
							}).catch((res) => {
								console.log(res)
							}).finally(err => {
								uni.hideLoading();
							})
							
							// this.$request('admin/fenghao', {
							// 	fenghaoid: item._id[0]._id
							// })
							// .then(res => {
							// 	uni.hideLoading()
							// 	this.loadData()
							// }).catch(err => {
							// 	uni.hideLoading()
							// 	console.log("err: ",err);
							// })
							
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			confirmOpenUser(item){
				console.log("item: ",item);
				uni.showModal({
					title: '提示',
					content: '此用户已注销账号，确定恢复账号吗？',
					showCancel: true,
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						console.log("res: ", res);
						if (res.confirm) {
							uni.showLoading({
								title: '处理中...',
								mask: false
							});
							uniCloud.callFunction({
								name: 'uni-admin',
								data: {
									action: "admin/openUser",
									data: {
										openUserId: item._id
									}
								}
							}).then((res) => {
								console.log(res, "update--------------")
								uni.hideLoading();
								this.loadData()
								uni.showToast({
									title: "操作成功",
									icon: 'none',
									duration: 2000
								})
							}).catch((res) => {
								console.log(res)
							}).finally(err => {
								uni.hideLoading();
							})
							
							// this.$request('admin/openUser', {
							// 	openUserId: item._id[0]._id 
							// })
							// .then(res => {
							// 	console.log("res: ",res);
							// 	uni.hideLoading()
							// 	this.loadData()
							// }).catch(err => {
							// 	uni.hideLoading()
							// 	console.log("err: ",err);
							// })
							
							
						}
					},
					fail: (err) => {
						console.log("err: ",err);
					},
					complete: (com) => {
						console.log("com: ",com);
					}
				});
			}
		}
	}
</script>
<style>
</style>
